package org.ssi.util;

import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.bjtech.domain.AssignMeterData;

public class WFDOMUtils {
	public List<AssignMeterData> parseMeterDataXml(String xmlStr)
			throws ParserConfigurationException, SAXException, IOException {
		List<AssignMeterData> dataList = new ArrayList<AssignMeterData>();
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		StringReader read = new StringReader(xmlStr);
		InputSource is = new InputSource(read);
		is.setEncoding("UTF-8");
		Document doc = db.parse(is);
		// 获得根元素结点
		NodeList meterDataNodeList = doc.getElementsByTagName("distributor");
		for (int i = 0; i < meterDataNodeList.getLength(); i++) {
			Node meterDataNode = meterDataNodeList.item(i);
			dataList.add(parseMeterDataNode(meterDataNode));
		}

		return dataList;
	}

	private AssignMeterData parseMeterDataNode(Node meterDataNode) {
		AssignMeterData meterData = new AssignMeterData();
		NodeList nodeList = meterDataNode.getChildNodes();
		String meterNum = "";
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node attrNode = nodeList.item(i);
			String attrName = attrNode.getNodeName();
			String attrValue = attrNode.getFirstChild().getNodeValue();
			if (attrName.equals("id")) {
				meterNum = attrValue;
			} else if (attrName.equals("addr")) {
				meterData.setJzq_num(attrValue);
			} else if (attrName.equals("frTmp")) {
				meterData.setFrom_temp(Float.valueOf(attrValue));
			} else if (attrName.equals("bkTmp")) {
				meterData.setBack_temp(Float.valueOf(attrValue));
			} else if (attrName.equals("curHot")) {
				meterData.setRead_num(Long.valueOf(attrValue));
			} else if (attrName.equals("UpdateTime")) {
				try {
					meterData.setRead_time(TimeFormatUtil.parseTime(attrValue));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else if (attrName.equals("m1")) {
				meterData.setJanuary(Long.valueOf(attrValue));
			} else if (attrName.equals("m2")) {
				meterData.setFebruary(Long.valueOf(attrValue));
			} else if (attrName.equals("m3")) {
				meterData.setMarch(Long.valueOf(attrValue));
			} else if (attrName.equals("m4")) {
				meterData.setApril(Long.valueOf(attrValue));
			} else if (attrName.equals("m5")) {
				meterData.setMay(Long.valueOf(attrValue));
			} else if (attrName.equals("m10")) {
				meterData.setOctober(Long.valueOf(attrValue));
			} else if (attrName.equals("m11")) {
				meterData.setNovember(Long.valueOf(attrValue));
			} else if (attrName.equals("m12")) {
				meterData.setDecember(Long.valueOf(attrValue));
			} else if (attrName.equals("lastYear")) {
				meterData.setLast_year_num(Long.valueOf(attrValue));
			} else if (attrName.equals("sendTime")) {
				try {
					meterData.setRecord_time(TimeFormatUtil.parseTime(attrValue));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			meterData.setSerious_num(meterData.getJzq_num()+meterNum);
		}
		return meterData;
	}


	public static void main(String[] args) throws ParserConfigurationException,
			SAXException, IOException {
		WFDOMUtils d = new WFDOMUtils();
		String xml="<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><project><Concentrator><id>XunYang</id><addr>00:11:67:E4:46:D3</addr><channel>00</channel><updateTime>02</updateTime><interval>01</interval><distributor><id>02</id><addr>000000000000</addr><frTmp>00</frTmp><bkTmp>00</bkTmp><curHot>000000</curHot><UpdateTime>110512020320</UpdateTime><UpdateTime>110512020320</UpdateTime><m1>111</m1><m2>111</m2><m3>111</m3><m4>111</m4><m5>111</m5><m10>000000</m10><m11>111</m11><m12>09111</m12><lastYear>111</lastYear><sendTime>121205120300</sendTime></distributor><distributor><id>03</id><addr>000000000000</addr><frTmp>00</frTmp><bkTmp>00</bkTmp><curHot>000000</curHot><UpdateTime>110512020320</UpdateTime><UpdateTime>110512020320</UpdateTime><m1>111</m1><m2>111</m2><m3>111</m3><m4>111</m4><m5>111</m5><m10>000000</m10><m11>111</m11><m12>1223</m12><lastYear>111</lastYear><sendTime>121205120300</sendTime></distributor><distributor><id>04</id><addr>000000000000</addr><frTmp>00</frTmp><bkTmp>00</bkTmp><curHot>000000</curHot><UpdateTime>110512020321</UpdateTime><UpdateTime>110512020321</UpdateTime><m1>111</m1><m2>111</m2><m3>111</m3><m4>111</m4><m5>111</m5><m10>000000</m10><m11>111</m11><m12>112</m12><lastYear>111</lastYear><sendTime>121205120300</sendTime></distributor></Concentrator></project>";
		 List<AssignMeterData> list = d.parseMeterDataXml(xml);
		 System.out.println(list);
	}
}
